package org.jash.shop2201.pay

import android.os.Bundle
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.MutableLiveData
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.alipay.sdk.app.PayTask
import org.jash.shop2201.R
import org.jash.shop2201.databinding.ActivityPayBinding
import org.jash.shop2201.mvvm.BaseActivity
import org.jash.shop2201.mvvm.BaseViewModel
import org.jash.shop2201.pay.util.OrderInfoUtil2_0
import java.util.concurrent.Executors

@Route(path = "/shop/pay")
class PayActivity : BaseActivity<ActivityPayBinding, BaseViewModel>() {
    @Autowired
    @JvmField
    var total = 0f
    @Autowired
    @JvmField
    var body = ""
    /**
     * 用于支付宝支付业务的入参 app_id。
     */
    val APPID = "9021000136678494"

    /**
     * 用于支付宝账户登录授权业务的入参 pid。
     */
    val PID = "2088721035210245"

    /**
     * 用于支付宝账户登录授权业务的入参 target_id。
     */
    val TARGET_ID = "sandbox 默认应用:2088721035210245"

    /**
     * pkcs8 格式的商户私钥。
     *
     * 如下私钥，RSA2_PRIVATE 或者 RSA_PRIVATE 只需要填入一个，如果两个都设置了，本 Demo 将优先
     * 使用 RSA2_PRIVATE。RSA2_PRIVATE 可以保证商户交易在更加安全的环境下进行，建议商户使用
     * RSA2_PRIVATE。
     *
     * 建议使用支付宝提供的公私钥生成工具生成和获取 RSA2_PRIVATE。
     * 工具地址：https://doc.open.alipay.com/docs/doc.htm?treeId=291&articleId=106097&docType=1
     */
    val RSA2_PRIVATE =
        "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDDoabdnPkGjRplw18c2lcBgEwktoRILUSWdPCIymlmUyPiBDCJeITI2EswKguCNDSbHNAke1vgqqe5PPLNzEoRFfxVvQSBFDeTudJAvf/f3veRPDZKgNy/DzFtUuWad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N4niKqvyCPwKBgC2aYAdREPkNNghkPhZmkRu7UbpUXZm8p6mhu+LxySTxrzEc6sGMFuQ8jftccuy8bf0nclT4JbQsZJbA5FLuGrB5tgwEgZfRb4BfW7VdCJ0H17a4eX1a0ju6EQndfxuI+ZnnopSJBRz8CBIjAfSzUdMVR1lZZ0xksDwR6MDh9TcRAoGBAIRQxq5Ku82DbsIYG69kfwxrg2uOAv/4bJ3efAR9D/EcUK3NWs6P54tR0oT/Xia/a+2yKATBGFxHXJc3LZ/4gHnysDEEI04ptCmfdCo7ieR1XQ7O5dSH5ux16FiZi9f/FDgOb+oloIQATWujIBzLhD0Zc2vspQHv0dgYSraaLaI7"
    val executor = Executors.newSingleThreadExecutor()
    val liveData = MutableLiveData<PayResult>()
    override fun initData() {
        ARouter.getInstance().inject(this)
        liveData.observe(this) {
            when(it.result) {
                "9000" -> Toast.makeText(this, "支付成功", Toast.LENGTH_SHORT).show()
                else -> Toast.makeText(this, "支付失败, 原因:${it.memo}", Toast.LENGTH_SHORT).show()
            }
        }
    }

    override fun initView() {
        setSupportActionBar(binding.toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        binding.total = total
        binding.body = body
        binding.pay.setOnClickListener {
            val map = OrderInfoUtil2_0.buildOrderParamMap(APPID, total, body, true)
            val order = OrderInfoUtil2_0.buildOrderParam(map)
            val sign = OrderInfoUtil2_0.getSign(map, RSA2_PRIVATE, true)
            val orderInfo = "$order&$sign"
            executor.execute {
                val task = PayTask(this)
                val result = task.payV2(orderInfo, true)
                liveData.postValue(PayResult(result))
            }

        }

    }

}